package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.applozic.mobicomkit.contact.AppContactService;
import com.bumptech.glide.Glide;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRedPacketLogResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;

/**
 * Created by ASUS on 2017/11/1.
 */

public class GetRedPacketAdapter extends BaseAdapter {
    private Context mContext;
    private GetRedPacketLogResponse getRedPacketLogResponses;
    private AppContactService appContactService;
    public GetRedPacketAdapter(Context context, GetRedPacketLogResponse getRedPacketLogResponses){
        mContext = context;
        this.getRedPacketLogResponses = getRedPacketLogResponses;
        appContactService = new AppContactService(context);
    }

    @Override
    public int getCount() {
        if (getRedPacketLogResponses == null) {
            return 0;
        }
        return getRedPacketLogResponses.getList().getGifts().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.get_packet_child_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

//        GlideHelper.loadImage(mContext, getRedPacketLogResponses.getList().getGifts().get(i).getAvatar(), holder.logo);
        Glide.with(mContext).load(getRedPacketLogResponses.getList().getGifts().get(i).getAvatar())
                .placeholder(R.mipmap.chat_default)
                .fallback(R.mipmap.chat_default)
                .into(holder.logo);
        holder.name.setText(appContactService.getContactById(getRedPacketLogResponses.getList().getGifts().get(i).getNickname()).getDisplayName());
        holder.time.setText(getRedPacketLogResponses.getList().getGifts().get(i).getUpdated_at());
        holder.money.setText( getRedPacketLogResponses.getList().getGifts().get(i).getAmount());
        if(getRedPacketLogResponses.getList().getGifts().get(i).getType().equals("3")){
            holder.pin.setVisibility(View.VISIBLE);
        } else {
            holder.pin.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.logo)
        CircleImageView logo;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.next)
        ImageView next;
        @BindView(R.id.money)
        TextView money;
        @BindView(R.id.pin)
        ImageView pin;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
